package com.leetcode.Stack;

import java.util.HashSet;
import java.util.Stack;

public class Leetcode150 {


    public int evalRPN(String[] tokens) {
        HashSet<String> ops = new HashSet<>();
        ops.add("+");
        ops.add("-");
        ops.add("*");
        ops.add("/");
        Stack<Integer> nums = new Stack<>();
        int result = 0;
        for (int i = 0; i < tokens.length; i++) {
            if (ops.contains(tokens[i])) {
                int num2 = nums.pop();
                int num1 = nums.pop();
                if (tokens[i].equals("+")) {
                    result = num1 + num2;
                } else if (tokens[i].equals("*")) {
                    result = num1 * num2;
                } else if (tokens[i].equals("/")) {
                    result = num1 / num2;
                } else if (tokens[i].equals("-")) {
                    result = num1 - num2;
                }
                nums.add(result);
            } else {
                nums.add(Integer.valueOf(tokens[i]));
            }
        }
        return nums.pop();
    }

    public static void main(String[] args) {
        String[] str = {"2", "1", "+", "3", "*"};
        System.out.println(new Leetcode150().evalRPN(str));
    }

}
